package com.zzyl.nursing.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.framework.config.properties.WechatProperties;
import com.zzyl.nursing.service.WechatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author robin
 * @date 25.5.26 10:48
 */
@Service
@RequiredArgsConstructor
public class WechatServiceImpl implements WechatService {
    // 本质是有参构造依赖注入：给成员变量赋值
    private final WechatProperties wechatProperties;
    
    @Override
    public String getOpenId(String code) {
        // 1-接口调用请求
        String code2SessionUrl="https://api.weixin.qq.com/sns/jscode2session";
        Map<String, Object> paramMap=MapUtil.<String, Object>builder()
                                            .put("appid",wechatProperties.getAppId())
                                            .put("secret",wechatProperties.getAppSecret())
                                            .put("js_code",code)
                                            .put("grant_type","authorization_code")
                                            .build();
        String resultJson=HttpUtil.get(code2SessionUrl,paramMap);
        // 2-响应结果处理
        // json转换java对象--->才能获取对象属性
        JSONObject resultObj=JSONUtil.parseObj(resultJson);
        // 如果包含了errcode说明请求接口失败
        if (resultObj.containsKey("errcode")) {
            throw new ServiceException("调用微信小程序获取openId接口请求失败");
        }
        return resultObj.getStr("openid");
    }
    
    @Override
    public String getAccessToken() {
        // 1-请求
        String getAccessTokenUrl="https://api.weixin.qq.com/cgi-bin/token";
        Map<String, Object> paramMap=MapUtil.<String, Object>builder()
                                            .put("appid",wechatProperties.getAppId())
                                            .put("secret",wechatProperties.getAppSecret())
                                            .put("grant_type","client_credential")
                                            .build();
        String resultJson=HttpUtil.get(getAccessTokenUrl,paramMap);
        // 2-响应
        JSONObject resultObj=JSONUtil.parseObj(resultJson);
        if (resultObj.containsKey("errcode")) {
            throw new ServiceException("调用微信小程序获取接口凭证失败");
        }
        return resultObj.getStr("access_token");
    }
    
    @Override
    public String getPhoneNumber(String accessToken,String phoneCode,String openId) {
        // 1-请求
        String getPhoneNumberUrl="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
        Map<String, Object> bodyMap=MapUtil.<String, Object>builder()
                                           .put("code",phoneCode)
                                           .put("openid",openId)
                                           .build();
        String resultJson=HttpUtil.post(getPhoneNumberUrl,JSONUtil.toJsonStr(bodyMap));
        // 2-响应
        JSONObject resultObj=JSONUtil.parseObj(resultJson);
        if (resultObj.getInt("errcode") != 0) {
            throw new ServiceException("调用微信小程序接口失败：" + resultObj.getStr("errmsg"));
        }
        JSONObject jsonObject=resultObj.getJSONObject("phone_info");
        return jsonObject.getStr("phoneNumber");
    }
}
